const app = getApp()
Page({
    data: {
      winHeight:896,
      flag: 0,
      currentTab: 0,
      goods_list:[],
      appraiseList:undefined,
      comments_list:[],
      vote:{},
      banggou: undefined,
      good_list:[
          {
              id:0,
              title:"蒙都风干牛肉干手撕原味休闲零食",
              price:99,
              imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
              sale: 335,//销量
              time_remaining:25,//剩余时间，记录小时
              day:0,
              
          },
          {
            id:1,
            title:"蒙都风干牛肉干手撕原味休闲零食",
            price:99,
            imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
            sale: 335,//销量
            time_remaining:23,//剩余时间，记录小时
            day:0,
            
        },
        {
            id:2,
            title:"蒙都风干牛肉干手撕原味休闲零食",
            price:99,
            imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
            sale: 335,//销量
            time_remaining:23,//剩余时间，记录小时
            day:0,
  
        },
        {
            id:3,
            title:"蒙都风干牛肉干手撕原味休闲零食",
            price:99,
            imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
            sale: 335,//销量
            time_remaining:23,//剩余时间，记录小时
            day:0
        },
        {
          id:4,
          title:"蒙都风干牛肉干手撕原味休闲零食",
          price:99,
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          sale: 335,//销量
          time_remaining:23,//剩余时间，记录小时
          day:0
      },
      {
        id:5,
        title:"蒙都风干牛肉干手撕原味休闲零食",
        price:99,
        imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
        sale: 335,//销量
        time_remaining:23,//剩余时间，记录小时
        day:0
    },
    
    
      ],
      vote1:{
          voteId:0,
          date:"2021-6-13",
          voteTitle:"日常必需品需求采纳，点赞超80下周就团购哦！",
          //votes_all:100,最多可投票数
          options:[
              {
                  optionId:0,
                  optionTitle:'金龙鱼东北大米5kg',
                  optionPic:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                  num:34,
                  speed:'34.12%',
                  collect: false
              },
              {
                optionId:1,
                optionTitle:'金龙鱼东北大米5kg',
                optionPic:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                num:50,
                speed:'50%',
                collect: false
            },
            {
                optionId:2,
                optionTitle:'金龙鱼东北大米5kg',
                optionPic:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                num:60,
                speed:'60%',
                collect: false
            },
            {
                optionId:3,
                optionTitle:'金龙鱼东北大米5kg',
                optionPic:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
                num:60,
                speed:'60%',
                collect: false
            },
            
          ]
      },
      //
      status: true, //评价框显示隐藏
      content: "",
      repotList: [
      {
        users: "山河",
        img: "../../../img/00.jpg",
        selected: "黑色#眼线笔01",
        select: [],
        praise: 0,
        change: false,
        comment: "颜值超高的一款眉笔!!!自然生动, 十分适合初学者~",
        picture: [
          "../../../img/00.jpg",
          "../../../img/00.jpg",
          "../../../img/00.jpg",
          "../../../img/00.jpg",
          "../../../img/00.jpg"
        ],
        time: "2020-05-29"
      },
    ],
      appraiseList: [
        {
          username: "太白子",
          img: "/images/banggou.jpg",
          is_merchant: 0,
          isOpen: false,
          change: false,
          praise: 0,
          appraise_content: "上期的大米可以再团一次吗🎺上期的大米可以再团一次吗🎺上期的大米可以再团一次吗🎺上期的大米可以再团一次吗",
          reply_list: [
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25"},
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
          ],
          time: "今天 15:07",
        },
        {
          username: "太白子",
          img: "/images/banggou.jpg",
          is_merchant: 1,
          isOpen: false,
          change: false,
          praise: 0,
          appraise_content: "我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬我想要食用油🚬",
          reply_list: [
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, time: "05-25", praise: 0, },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" },
            { nickname: "太白金星", img: "/images/banggou.jpg", reply_content: "我也是这么觉得", changes: false, praise: 0, time: "05-25" }
          ],
          time: "今天 15:07",
        },
        
      ],
      // userpingfen: [         
      //   { pingfen: 4 }
      // ],
      comment_list: [
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        },
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        },
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        },
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        }
        ,
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        }
        ,
        {
          good_id:2345,
          title:"梦都风干牛肉干手撕原味休闲零食",
          imageUrl:'https://z3.ax1x.com/2021/06/14/2786G6.jpg',
          account: 134,//评论数量
          sale: 300,//销量
          date: "2021-6-1",
          score: 4.3
        }

      ],

    },
    
    switchNav: function(e) {
      var page = this;
      var id = e.target.id;
      if (this.data.currentTab == id) {
        return false;
      } else {
        page.setData({
          currentTab: id
        });
      }
      page.setData({
        flag: id
      });
    },
    catchTouchMove: function (res) {
      return false
    },
    toCollect: function(e) {
      var index = e.currentTarget.dataset.id;
      console.log(index);
      var s = 'vote.options[' + index + '].collect';
      var optionId = this.data.vote.options[index].optionId;
      var that = this;

      console.log(optionId);
      var bol = this.data.vote.options[index].collect; // 获取状态
      if(bol == false){
      
      let data = {
        optionId: optionId
      }
      app.sendPostRequest('/community/helphelp/vote', data,(res) => {
        if(res.code == 200) {
          wx.showToast({
              title: '投票成功！',  
              icon: 'success',  
              duration: 1500
          })
          this.setData({
            [s]: true
          })
          this.getVote();
          // that.setData({
            
          // })
        } else {
          wx.showToast({
              title: '投票失败！',  
              icon: 'error',  
              duration: 1500
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
    }
    if(bol == true){
      wx.showModal({
        title: '提示',
        content: '取消投票？',
        success: function (res) {
          if (res.confirm) {//这里是点击了确定以后  
            console.log('用户点击确定')
            let data = {
              optionId: optionId
            }
            
            app.sendPostRequest('/community/helphelp/voteDelete', data,(res) => {
              if(res.code == 200) {
                // that.setData({
                //   [s]: false
                // })
                wx.showToast({
                    title: '取消投票成功！',  
                    icon: 'success',  
                    duration: 1500
                })
              } else {
                wx.showToast({
                    title: '取消失败！',  
                    icon: 'error',  
                    duration: 1500
                })
              }
              that.getVote();
            }, (err) => {
              console.log(err.errMsg)
            })
   
          } else {//这里是点击了取消以后
   
            console.log('用户点击取消')
   
          }
   
        }
   
      })
    }
      },
      
    // 标题关键字搜索
  search: function (e){
    // 搜索字段
    var searchtext = this.data.inputVal
    var posts_list = this.data.posts_list
    var v_posts_list = []
    for(var i=0; i<posts_list.length; i++){
      var temp = posts_list[i].title.indexOf(searchtext)
      if(temp!=-1){
        v_posts_list.push(posts_list[i])
      }
    }
    this.setData({
      visible_posts_list: v_posts_list
    })
  },

  // 点击菜单栏触发的事件函数
  handleClick: function (e) {
    const index = e.currentTarget.dataset.id;
    const { selectItem, navItem } = this.data;
    // 点击事件，开始时一定会执行的，先令所有下拉选项先隐藏
    const promise = new Promise((res) => {
      this.setData({
        showOrHide: false
      })
      res()
    })
    promise.then(() => {
      // 定义一个延迟0.1秒的函数，和函数动画事件对上
      setTimeout(() => {
        // 当点击的是已经点开的菜单项，则隐藏
        if (this.data.listNum === index) {
          navItem[index].isActive = false
          // 重置所有样式
          this.setData({
            navItem,
            listNum: -1
          })
          return;
        } else {
          // 否则就令当前的index = listNum方便下一次判断
          this.setData({
            listNum: index
          })
        }
        // 令当前点击的菜单栏高亮
        navItem.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        this.setData({
          showOrHide: true,
          selectedItem: selectItem[index].rank,
          navItem
        })
      }, 100)
    })
  },
  showInput: function () {
    this.setData({
        inputShowed: true
    });
  },
  hideInput: function () {
    var v_posts_list = this.data.posts_list
    this.setData({
        inputVal: "",
        inputShowed: false,
        visible_posts_list: v_posts_list
    });
  },
  clearInput: function () {
    this.setData({
        inputVal: ""
    });
  },
  inputTyping: function (e) {
      this.setData({
          inputVal: e.detail.value
      });
  },
  // bbbb() {
  //   wx.navigateTo({
  //     url: `../tuan/test/test`
  //   })
  // },
  //失去焦点时获取里面评论内容
  bindTextAreaBlur: function (e) {
    this.setData({
      content: e.detail.value,
    })
  },
  //点击按钮时得到里面的值
  fabiao: function (e) {
    if(this.data.content == '') {
      wx.showToast({
        title: '内容不能为空',
        icon: "none",
        duration: 1500,
      })
    }else {
      this.setData({
        focus: 'false',
        concent1: this.data.content,
      })
      console.log(this.data.content)
    }
  },
  /**
   * 点击回复显示隐藏评价框
   */
  // chengeStatusTop: function() {
  //   let status = this.data.status;
  //   this.setData({
  //     status: !status
  //   })
  // },
  // 点赞功能逻辑
  praiseThis: function (e) {
    var index = e.currentTarget.dataset.curindex;
    if (this.data.repotList[index]) {
      var change = this.data.repotList[index].change;
      if (change !== undefined) {
        var num = this.data.repotList[index].like;
        if (change) {
          this.data.repotList[index].like = (num - 1);
          this.data.repotList[index].change = false;
          app.sendPostRequest('/community/helphelp/deleteVoteComment1Like',this.data.repotList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '取消成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '取消失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        } else {
          this.data.repotList[index].like = (num + 1);
          this.data.repotList[index].change = true;
          app.sendPostRequest('/community/helphelp/voteComment1Like', this.data.repotList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '点赞成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '点赞失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        }
        this.setData({
          repotList: this.data.repotList
        })
      }
    }
  },
  // 点击展开
  // chooseUnfold: function(e) {
  //   var key = e.currentTarget.dataset.key;
  //   var val = e.currentTarget.dataset.value;
  //   key = key + '.isOpen';
  //   this.setData({
  //     [key]: !val
  //   })
  // },
  // 点赞功能逻辑s
  praiseThiss: function (e) {
    var index = e.currentTarget.dataset.curindex;
    if (this.data.appraiseList[index]) {
      var change = this.data.appraiseList[index].change;
      if (change !== undefined) {
        var num = this.data.appraiseList[index].like;
        if (change) {
          this.data.appraiseList[index].like = (num - 1);
          this.data.appraiseList[index].change = false;
          app.sendPostRequest('/community/helphelp/deleteVoteComment1Like', this.data.appraiseList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '取消成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '取消失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
        } else {
          this.data.appraiseList[index].like = (num + 1);
          this.data.appraiseList[index].change = true;
          app.sendPostRequest('/community/helphelp/voteComment1Like', this.data.appraiseList[index],(res) => {
            if(res.code == 200) {
              wx.showToast({
                  title: '点赞成功！',  
                  icon: 'success',  
                  duration: 1500
              })
            } else {
              wx.showToast({
                  title: '点赞失败！',  
                  icon: 'error',  
                  duration: 1500
              })
            }
          }, (err) => {
            console.log(err.errMsg)
          })
          
        }
        this.setData({
          appraiseList: this.data.appraiseList
        })
      }
    }
  },
  // 点赞内层逻辑
  // praiseThisss: function(e) {
  //   var index = e.currentTarget.dataset.curindex;
  //   var indexs = e.currentTarget.dataset.curindexs;
  //   console.log(indexs)
  //   if (this.data.appraiseList[index].reply_list[indexs]) {
  //     var change = this.data.appraiseList[index].reply_list[indexs].changes;
  //     if (change !== undefined) {
  //       var num = this.data.appraiseList[index].reply_list[indexs].praise;
  //       if (change) {
  //         this.data.appraiseList[index].reply_list[indexs].praise = (num - 1);
  //         this.data.appraiseList[index].reply_list[indexs].changes = false;
  //       } else {
  //         this.data.appraiseList[index].reply_list[indexs].praise = (num + 1);
  //         this.data.appraiseList[index].reply_list[indexs].changes = true;
  //       }
  //       this.setData({
  //         appraiseList: this.data.appraiseList
  //       })
  //     }
  //   }
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //var _this = this;
    //var tiyan = this.data.userpingfen;
    // for (var i = 0; i < tiyan.length; i++) {
    //   tiyan[i].pingfenpic = pingxin.pingfen(parseFloat(tiyan[i].pingfen));    //使用函数
    // }
    // _this.setData({
    //   userpingfen: tiyan
    // })
    this.getBanggou();
    this.getlist();
    this.getlist1();
    this.getVote();
    this.getComment();

   
  },

  getBanggou:function(){
    app.sendGetRequest('/community/helphelp/info', (res) => {

      this.setData({
        banggou: res.data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  getVote: function(){
    app.sendGetRequest('/community/helphelp/voteInfo', (res) => {

      this.setData({
        vote: res.data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
   
   
  },
  getComment: function(){
    
      app.sendGetRequest('/community/helphelp/jingxuanComments', (res) => {
        console.log(res)
        this.setData({
          appraiseList: res.data
        })
      }, (err) => {
        console.log(err.errMsg)
      })
    

  },
  getlist: function(){
    var that = this;
    that.data.token = app.getToken()
    console.log(that.data.token)
    //网络请求获取账单数据
    // wx.request({
    //   url: 'http://localhost:8080/community/helphelp/goodslistTop',
    //   method:'GET',
    //   header:{
    //     'content-type': 'application/json',
    //     'Authorization':  that.data.token
    //   },
    //   success:function(res){
    //     // console.log("userInfo" + JSON.stringify(res))
    //     console.log(res.data);
    //     that.setData({
    //       goods_list: res.data.data
    //     });
    //   },
    // }) 
    app.sendGetRequest("/community/helphelp/goodslistTop",(res) => {
      console.log(res);
        that.setData({
          goods_list: res.data
        });
    })

  },

  getlist1: function(){
    var that = this;
    that.data.token = app.getToken()
    console.log(that.data.token)
    //网络请求获取账单数据
    // wx.request({
    //   url: 'http://localhost:8080/community/helphelp/xiajiaTop',
    //   method:'GET',
    //   header:{
    //     'content-type': 'application/json',
    //     'Authorization':  that.data.token
    //   },
    //   success:function(res){
    //     // console.log("userInfo" + JSON.stringify(res))
    //     console.log(res.data);
    //     that.setData({
    //       comments_list: res.data.data
    //     });
    //   },
    // }) 
    app.sendGetRequest("/community/helphelp/xiajiaTop", (res)=>{
      console.log(res);
        that.setData({
          comments_list: res.data
        });
    })

  },
  //进入商品详情页
  detail: function(e){
      var id = e.currentTarget.dataset.id;
      console.log(id);
      wx.navigateTo({
        url: `../tuan/goodDetail/goodDetail?id=`+id
      })
  },
  //进入商品详情页
  commentDetail: function(e){
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `../tuan/commentDetail/commentDetail?id=`+id
    })
},

//进入历史需求采纳
lishi: function(){
  wx.navigateTo({
    url: "../tuan/command/command"
  })
},
moreComment: function(){
  wx.navigateTo({
    url: "../tuan/moreComment/moreComment?voteId="+this.data.vote.voteId
  })
},
moreGoods: function(){
  wx.navigateTo({
    url: "../tuan/moreGoods/moreGoods"
  })
},
moreLishi: function(){
  wx.navigateTo({
    url: "../tuan/moreLishi/moreLishi"
  })
}
  })
  